package org.jwcloud.project.module.zx.txdpStatic.controller;

import java.io.IOException;
import java.math.BigDecimal;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.jwcloud.project.module.zx.baseManager.service.repository.MatBaseManagerRepository;
import org.jwcloud.project.module.zx.common.FmtUtil;
import org.jwcloud.project.module.zx.common.Resp;
import org.jwcloud.project.module.zx.common.repository.RptCommonRepository;
import org.jwcloud.project.module.zx.common.service.RptUtService;
import org.jwcloud.project.module.zx.txzldp.service.repository.TxzldpCommonViewRepository;
import org.jwcloud.project.module.zx.workTime.service.repository.MatWorkTimeRepository;
import org.mybatis.spring.SqlSessionTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import com.github.pagehelper.PageHelper;
import com.jw.mid.mm.comm.common.controller.BaseController;
import com.jw.mid.mm.comm.common.controller.CrudController;
import com.jw.mid.mm.comm.common.json.Response;
import com.jw.mid.mm.comm.common.pagination.ListBounds;
import com.jw.mid.mm.comm.common.pagination.ListSlice;
import com.jw.mid.mm.comm.common.pagination.PageListSlice;
import com.jw.mid.mm.comm.common.pagination.Pagination;
import com.jw.mid.mm.comm.common.service.CrudService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(value = "API - Controlle",
description = "铁西监督治理大屏  结果数据", tags = "jdzl")
@RestController
@Transactional
@RequestMapping("/rest/txdpStatic/TxdpStatic")
public class TxdpStaticController extends BaseController{
	
	@Autowired
    private SqlSessionTemplate sqlSessionTemplate;
	
	@Autowired
    public RptUtService rptUtService;
	
	private static Map<String, String> tableMap = new HashMap();
	private static Map<String, String> sqlMap = new HashMap();
	
	static {
		tableMap.put("三重一大_任免_季度", "三重一大_任免_季度");
		tableMap.put("三重一大_决策_季度", "三重一大_决策_季度");
		tableMap.put("农村类支出", "农村类支出");
		tableMap.put("决策数量前十", "决策数量前十");
		tableMap.put("办公费资金支出", "办公费资金支出");
		tableMap.put("各类资金走势图", "各类资金走势图");
		tableMap.put("社区类支出", "社区类支出");
		tableMap.put("补贴类资金支出", "补贴类资金支出");
		tableMap.put("货物及服务类支出", "货物及服务类支出");
		tableMap.put("项目数量前五", "项目数量前五");
		tableMap.put("项目资金走势图", "项目资金走势图");
		tableMap.put("项目金额前五", "项目金额前五");
		tableMap.put("项目金额变更", "项目金额变更");
		tableMap.put("首页_资金信息", "首页_资金信息");
		tableMap.put("首页_项目信息", "首页_项目信息");
		tableMap.put("首页_三重一大", "首页_三重一大");
		tableMap.put("首页_问题类别", "首页_问题类别");
		
		tableMap.put("v_问题_大数据监督问题信息", "v_问题_大数据监督问题信息");
		tableMap.put("v_问题_纪律监督_问题及资金", "v_问题_纪律监督_问题及资金");
		tableMap.put("v_问题_专项监督_问题及资金", "v_问题_专项监督_问题及资金");
		
		tableMap.put("v_问题_了解关注数", "v_问题_了解关注数");
		tableMap.put("v_问题_移交纪委数", "v_问题_移交纪委数");
		tableMap.put("v_问题_处置人数", "v_问题_处置人数");
		tableMap.put("v_问题_案件形态", "v_问题_案件形态");
		tableMap.put("v_项目_汇总", "v_项目_汇总");
		
		tableMap.put("v_专项监督类别", "v_专项监督类别");
		tableMap.put("v_纪律监督类别", "v_纪律监督类别");
		tableMap.put("v_各类资金走势图", "v_各类资金走势图");
		
		tableMap.put("v_各类资金汇总", "v_各类资金汇总");
		tableMap.put("v_三重一大数据汇总", "v_三重一大数据汇总");
		
		sqlMap.put("sql_案件处置人类别占比", "sql_案件处置人类别占比");
		sqlMap.put("sql_案件形态占比", "sql_案件形态占比");
		
		tableMap.put("项目金额变更次数分布", "项目金额变更次数分布");
		
	}
	 
	@GetMapping("queryCommonData")
    @ApiOperation(value = "", notes = "")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "table_name", value = "table_name", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "order", value = "order", required = false, dataType = "string", paramType = "query"),
    })
    public Resp queryCommonData(
    		@RequestParam(required = false) Map<String,String> param
    		){
		String tableName = param.get("table_name");
		if(!tableMap.containsKey(tableName) && !sqlMap.containsKey(tableName)) {
			return Resp.error("table [" + tableName + "] is not exists!");
		}
		
		if(param.containsKey("order") && StringUtils.isNotEmpty(param.get("order"))) {
			String order = param.get("order");
			if(order.replaceAll("\\s", "").length() != order.length()) {
				return Resp.error("检测到非法字符");
			}
			order = order.replace(":", " ").replace(";", ",");
			param.put("orderSql", "order by " + order);
		}
		
		List<Map> list;
		if(sqlMap.containsKey(tableName)) {
			//手写
			list = this.sqlSessionTemplate.selectList("txdpStatic.txdpStatic." + tableName, param);
		}else {
			//自动生成
			list = this.sqlSessionTemplate.selectList("txdpStatic.txdpStatic.queryCommonData", param);
			handle(list);
		}
		
		
		return Resp.success(list);
    }
	
	private List handle(List<Map> list) {
		if(list == null || list.size() == 0)
			return list;
		
		for(Map map: list) {
			if(map.containsKey("money")) {
				String money = map.get("money").toString();
				Double dmoney = Double.valueOf(money) / 10000;
				
				BigDecimal bd = new BigDecimal(dmoney);  
				Double res = bd.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
				map.put("money_yi", res);
			}
		}
		
		return list;
	}
	
	
	@GetMapping("queryCommonData_new")
    @ApiOperation(value = "新的静态数据接口  纵表", notes = "新的静态数据接口  纵表")
    @ApiImplicitParams({
    	@ApiImplicitParam(name = "table_name", value = "table_name", required = true, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "year_id", value = "year_id", required = false, dataType = "string", paramType = "query"),
    	@ApiImplicitParam(name = "limit", value = "查询数量 默认10", required = false, dataType = "int", paramType = "query"),
    	@ApiImplicitParam(name = "order", value = "order", required = false, dataType = "string", paramType = "query"),
    })
    public Resp queryCommonData_new(
    		@RequestParam(required = false) Map<String,String> param
    		){
		
		if(param.containsKey("order") && StringUtils.isNotEmpty(param.get("order"))) {
			String order = param.get("order");
			if(order.replaceAll("[\\s\\\\/\\*\\?\\\"<>\\|]", "").length() != order.length()) {
				return Resp.error("检测到非法字符");
			}
			order = order.replace(":", " ").replace(";", ",");
			param.put("_orderSql", "order by " + order);
		}
		
		if(StringUtils.isEmpty(param.get("limit"))) {
			param.put("limit", "10");
		}
		String limitSql = " limit " + param.get("limit");
		param.put("_limitSql", limitSql);
		
		List<Map> list;
		list = this.sqlSessionTemplate.selectList("txdpStatic.txdpStatic.queryCommonData_new", param);
		
		return Resp.success(list);
    }
	
}